
var dayNames 	= new Array();
var monthNames 	= new Array();
var dayDistribution = new Array();
var totalMonthNumber = 0;
var totalDays		 = 0;
var moonNames		 = new Array();
var totalMoons		 = 0;
var moonsNames  	 = new Array();
var monthFullMoon	 = new Array();
var dayFullMonth	 = new Array();

function addMoonNameField(increment){
	var obj = $('#moonsName');
	obj.html('');
	var numberOfMoons = $('#moonNumberInput').val()/1;
	numberOfMoons += increment;
	$('#moonNumberInput').val(numberOfMoons);
	
	var table = new jTABLE();
	var tr = new jTR();
	var th1 = new jTH();
	th1.setValue('{name}');
	th1.setStyle('width:10px;');
	var th2 = new jTH();
	th2.setValue('{full_month}');
	var th3 = new jTH();
	th3.setValue('{full_day}');
	tr.addCell(th1);
	tr.addCell(th2);
	tr.addCell(th3);
	table.addRow(tr);
	
	for (var i=0;i<numberOfMoons;i++){
		var tr = new jTR();
		var td1 = new jTD();
		var td2 = new jTD();
		var td3 = new jTD();
		
		var input = $('<input/>', {
					  'type': 'text',
					  'size': '5',
					  'name': 'moonsNames['+i+'][name]',
					  'onKeyUp':'javascript:moonsNames['+i+'] = this.value;',
					  'value':(moonsNames[i] != null ) ? moonsNames[i] : ""
					});
		
		td1.setValue(input);
		
		var input = $('<input/>', {
					  'type': 'text',
					  'size': '2',
					  'name': 'moonsNames['+i+'][month]',
					  'onKeyUp':'javascript:monthFullMoon['+i+'] = parseInt(this.value);',
					  'value':(monthFullMoon[i] != null ) ? monthFullMoon[i] : "1"
					});
		td2.setValue(input);
		
		var input = $('<input/>', {
					  'type': 'text',
					  'size': '2',
					  'name': 'dayFullMonth['+i+'][day]',
					  'onKeyUp':'javascript:dayFullMonth['+i+'] = parseInt(this.value);',
					  'value':(dayFullMonth[i] != null ) ? dayFullMonth[i] : "1"
					});
		td3.setValue(input);
		
		tr.addCell(td1);
		tr.addCell(td2);
		tr.addCell(td3);
		table.addRow(tr);
	}	
	obj.append(table.getTABLE());
}


 function addDayNameField(increment){
	var obj = $('#daysName');
	obj.html('');
	
	//Updating totals day
	var numberOfDays = $('#weeksDay').val()/1;
	numberOfDays += increment;
	$('#weeksDay').val(numberOfDays);
	
	var table = new jTABLE();
	var tr = new jTR();
	
	var th1 = new jTH();
	th1.setValue('n.');
	th1.setStyle('width:10px;');
	var th2 = new jTH();
	th2.setValue('{name}');
	tr.addCell(th1);
	tr.addCell(th2);
	table.addRow(tr);
	
	//var table = $('<table/>',{'class':'calendarInfo'});
	
	for (var i=0;i<numberOfDays;i++){
		var tr = new jTR();
		
		var td1 = new jTD();
		var input = $('<input/>', {
					  'type': 'text',
					  'size': '1',
					  'readonly': 'readonly',
					  'value':i
					});
		td1.setValue(input);
		
		var td2 = new jTD();
		var input = $('<input/>', {
			  'type': 'text',
			  'id':'dayName'+i,
			  'name': 'dayNames['+i+']',
			  'onKeyUp': 'javascript:dayNames['+i+'] = this.value;',
			  'value':(dayNames[i] != null ) ? dayNames[i] : ""
			});
		td2.setValue(input);
		
		tr.addCell(td1);
		tr.addCell(td2);
		table.addRow(tr);
		/*
		var br = $(document.createElement("BR"));
		obj.append(br);
		*/
	}
	obj.append(table.getTABLE());
 }
 
 function addMonthNameField(increment){
		var obj = $('#monthName');
		obj.html('');
		var numberOfMonth = $('#monthNumberInput').val()/1;
		numberOfMonth += increment;
		$('#monthNumberInput').val(numberOfMonth);
		
		var table = new jTABLE();
		var tr = new jTR();
		var th1 = new jTH();
		th1.setValue('n.');
		th1.setStyle('width:10px;');
		var th2 = new jTH();
		th2.setValue('{name}');
		var th3 = new jTH();
		th3.setValue('{days}');
		tr.addCell(th1);
		tr.addCell(th2);
		tr.addCell(th3);
		table.addRow(tr);
		
		for (var i=0;i<numberOfMonth;i++){
			var tr = new jTR();
			var td1 = new jTD();
			var td2 = new jTD();
			var td3 = new jTD();
			
			var input = $('<input/>', {
						  'type': 'text',
						  'size': '1',
						  'readonly': 'readonly',
						  'value':i
						});
			td1.setValue(input);
			
			var input = $(document.createElement("INPUT"));			
			input.attr ("type","text");
			input.attr ("name","monthsNames["+i+"]");
			input.attr ("onKeyUp","javascript:monthsNames["+i+"] = parseInt(this.value); totalYearDays();");
			input.val ((monthNames[i] != null ) ? monthNames[i] : "");
			td2.setValue(input);
			
			var input = $(document.createElement("INPUT"));
			input.attr ("type","text");
			input.attr ("size","3");
			//setting a default months day
			dayDistribution[i] = (dayDistribution[i] == undefined) ? 30 : dayDistribution[i];
			input.attr ("value",dayDistribution[i]);
			input.attr ("name","dayDistribution["+i+"]");
			input.attr ("onKeyUp","javascript:dayDistribution["+i+"] = parseInt(this.value); totalYearDays();");
			input.val((dayDistribution[i] != null ) ? dayDistribution[i] : "" );
			td3.setValue(input);
			
			tr.addCell(td1);
			tr.addCell(td2);
			tr.addCell(td3);
			table.addRow(tr);
		}			
		obj.append(table.getTABLE());
 }
 
 function totalYearDays(){
	 totalMonthNumber = $('#monthNumberInput').val()/1;
	 var total = 0;
	 for (var i=0;i<totalMonthNumber;i++)
		 	total += dayDistribution[i];
	 
	 var obj = $('#yearDaysInput');
	 obj.val(total);
 }

 function myDay(dayIndex){
	 for (var i=0;i<totalDays;i++){
	 	var obj = $('#dayName'+i);
	 	if (i == dayIndex)
	 		obj.style.border = "2px solid #990000";
	 	else 
	 		obj.style.border = "1px solid gray";
 	 }
 }